package com.meizhuang.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.meizhuang.base.BaseController;
import com.meizhuang.entity.Entry;
import com.meizhuang.entity.EntryAudit;
import com.meizhuang.param.request.PageRequest;
import com.meizhuang.result.JsonResult;
import com.meizhuang.result.PageResult;
import com.meizhuang.service.EntryAuditService;
import com.meizhuang.service.EntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping(value = "/entry")
public class EntryController extends BaseController {

    @Autowired
    private EntryService entryService;

    @Autowired
    private EntryAuditService entryAuditService;


    @RequestMapping("/list")
    public PageResult<Entry> list(PageRequest pageRequest) {

        Page<Entry> page = new Page(pageRequest.getPage(), pageRequest.getLimit(), pageRequest.getSortField(), pageRequest.getAscState());
        entryService.selectPage(page);

        return PageResult.buildSuccess(page.getTotal(), page.getRecords());

    }

    @RequestMapping("/update")
    public JsonResult update(Entry entry) {

        Map<String, Object> retMap = new LinkedHashMap<>();

        if (null == entry.getId()) {
            retMap.put("msg", "词条ID为空");
            return JsonResult.buildError(retMap);
        }

        if (null == entry.getUid()) {
            retMap.put("msg", "审核的商户ID为空");
            return JsonResult.buildError(retMap);
        }

        if (null == entry.getOfferProductId()) {
            retMap.put("msg", "审核商户报价产品ID为空");
            return JsonResult.buildError(retMap);
        }

        Lock lock = new ReentrantLock();
        try {
            lock.lock();
            EntityWrapper<Entry> entryEntityWrapper = new EntityWrapper();
            entryEntityWrapper.eq("id", entry.getId());
            entryEntityWrapper.eq("uid", entry.getUid());
            Entry queryEntry = entryService.selectOne(entryEntityWrapper);

            if (null == queryEntry) {
                retMap.put("msg", "审核的词条不存在");
                return JsonResult.buildError(retMap);
            }

            EntityWrapper<EntryAudit> entryAuditEntityWrapper = new EntityWrapper();
            entryAuditEntityWrapper.eq("uid", entry.getUid());
            entryAuditEntityWrapper.eq("offer_product_id", entry.getOfferProductId());
            EntryAudit queryEntryAudit = entryAuditService.selectOne(entryAuditEntityWrapper);

            if (null != queryEntryAudit) {

                entryAuditService.deleteById(queryEntryAudit.getId());

            }

            Date now = new Date();

            EntryAudit entryAudit = new EntryAudit(entry);
            entryAudit.setStatus(2);
            entryAudit.setUpdateTime(now);
            entryAuditService.insert(entryAudit);


            queryEntry.setStatus(2);
            queryEntry.setUpdateTime(now);
            entryService.updateById(queryEntry);
        } catch (Exception e) {
            retMap.put("msg", e.getMessage());
            return JsonResult.buildError(retMap);
        } finally {
            lock.unlock();
        }

        return JsonResult.buildSuccess(retMap);
    }


    /**
     * 批量审核接口
     *
     * @param entryArrayList
     * @return
     */
    @RequestMapping("/update_list")
    public JsonResult update_list(ArrayList<Entry> entryArrayList) {

        Map<String, Object> retMap = new LinkedHashMap<>();


        Lock lock = new ReentrantLock();
        try {
            lock.lock();

            entryArrayList.stream().forEach(entry -> {

                EntityWrapper<Entry> entryEntityWrapper = new EntityWrapper();
                entryEntityWrapper.eq("id", entry.getId());
                entryEntityWrapper.eq("uid", entry.getUid());
                Entry queryEntry = entryService.selectOne(entryEntityWrapper);

                if (null != queryEntry) {

                    EntityWrapper<EntryAudit> entryAuditEntityWrapper = new EntityWrapper();
                    entryAuditEntityWrapper.eq("uid", entry.getUid());
                    entryAuditEntityWrapper.eq("offer_product_id", entry.getOfferProductId());
                    EntryAudit queryEntryAudit = entryAuditService.selectOne(entryAuditEntityWrapper);

                    if (null != queryEntryAudit) {

                        entryAuditService.deleteById(queryEntryAudit.getId());

                    }

                    Date now = new Date();

                    EntryAudit entryAudit = new EntryAudit(entry);
                    entryAudit.setStatus(2);
                    entryAudit.setUpdateTime(now);
                    entryAuditService.insert(entryAudit);


                    queryEntry.setStatus(2);
                    queryEntry.setUpdateTime(now);
                    entryService.updateById(queryEntry);

                }

            });

        } catch (Exception e) {
            retMap.put("msg", e.getMessage());
            return JsonResult.buildError(retMap);
        } finally {
            lock.unlock();
        }

        return JsonResult.buildSuccess(retMap);
    }


    @RequestMapping("/delete")
    public JsonResult delete(List<Integer> ids) {

        Map<String, Object> retMap = new LinkedHashMap<>();
        entryService.deleteBatchIds(ids);

        return JsonResult.buildSuccess(retMap);
    }

}
